package mest.service.LGF.WarehousePurchase.Impl;

import mest.VO.LGF.WarehousePurchase.warehousesave_manytomanyVO;
import mest.VO.LGF.WarehousePurchase.warehouse_productjob_purchase_recordVO;
import mest.VO.LGF.WarehousePurchase.warehousesave_manytomany_recordVO;
import mest.dao.LGF.WarehousePurchase.IWarehousePurchaseDao;
import mest.entity.warehousesave_manytomany;
import mest.service.LGF.WarehousePurchase.IWarehousePurchaseService;
import mest.util.mestUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @auther Machenike
 * @create 2019-12-20 13:53
 */

@Service
public class WarehousePurchaseServiceImpl implements IWarehousePurchaseService {
    @Resource
    HttpServletRequest request;
    //    String theirDb = mestUtil.getCompanyDB(request);
    @Resource
    private IWarehousePurchaseDao iwpd;


    @Override
    public Map baseSelectNofinish(int index, int pageSize) {
        String theirDb = mestUtil.getCompanyDB(request);
        Map map = new HashMap();
        int count = iwpd.countNOFinish(theirDb);

        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",iwpd.selectAllNOFinish(theirDb,(index-1)*pageSize,pageSize));
        return map;
    }

    @Override
    public Map baseSelectFinish(int index, int pageSize) {
        String theirDb = mestUtil.getCompanyDB(request);
        Map map = new HashMap();
        int count = iwpd.countFinish(theirDb);

        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",iwpd.selectAllFinish(theirDb,(index-1)*pageSize,pageSize));
        return map;
    }

    @Override
    public Map selectPurchaseCondition(int wpId, int index, int pageSize) {
        String theirDb = mestUtil.getCompanyDB(request);
        Map map = new HashMap();
        int count = iwpd.selectPurchaseConditionCount(theirDb,wpId);

        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",iwpd.selectPurchaseCondition(theirDb,wpId,(index-1)*pageSize,pageSize));
        return map;
    }


    @Override
    public int addProductJobRecord(warehouse_productjob_purchase_recordVO wppr) {
        String theirDb = mestUtil.getCompanyDB(request);
        wppr.setTheirDb(theirDb);
        int i = -2;
        int i1 = -2;
        int i2 = -2;
        int i3 = -2;
        int i4 = -2;
        int i31 = -2;
        int i41 = -2;
        int flag = 1;
        try {
             i = iwpd.addReocrd(wppr);
             i1 = iwpd.updatehasBeenNumber(wppr);

             //更新warehousesave_manytomany   和 warehousesave_manytomany_record表
            warehousesave_manytomany wsmtm = iwpd.findWSMTMbysfIdAndWfId(theirDb,wppr.getWfId(),wppr.getSfId());
            if(wsmtm == null){

//                useGeneratedKeys="true" keyProperty="userId"
                //数据库wsmtm没联系，包装联系类
                warehousesave_manytomanyVO wsmtm1 = new warehousesave_manytomanyVO();
                wsmtm1.setWfId(wppr.getWfId());
                wsmtm1.setSfId(wppr.getSfId());
                wsmtm1.setNumber(wppr.getThisNumber());
                wsmtm1.setTheirDb(theirDb);

                //根据联系类插入
                //先插入warehousesave_manytomany表
                 i2 = iwpd.addWSMTM( wsmtm1);

                warehousesave_manytomany_recordVO wsmtmrVO = new warehousesave_manytomany_recordVO();
                wsmtmrVO.setWsmId(wsmtm1.getId());
                wsmtmrVO.setStatus("入库");
                wsmtmrVO.setNumber(wppr.getThisNumber());
                wsmtmrVO.setEfId(wppr.getEfId());
                wsmtmrVO.setTheirDb(theirDb);

                //再插入warehousesave_manytomany_record 记录表
                 i3 = iwpd.addWSMTMRecord( wsmtmrVO);
                 i31 = iwpd.updateWSMTMNumber(wsmtmrVO);
            }else{
                flag = 2;
                warehousesave_manytomany_recordVO wsmtmrVO = new warehousesave_manytomany_recordVO();
                wsmtmrVO.setWsmId(wsmtm.getId());
                wsmtmrVO.setStatus("入库");
                wsmtmrVO.setNumber(wppr.getThisNumber());
                wsmtmrVO.setEfId(wppr.getEfId());
                wsmtmrVO.setTheirDb(theirDb);
                i4 = iwpd.addWSMTMRecord( wsmtmrVO);
                 i41 = iwpd.updateWSMTMNumber(wsmtmrVO);
            }

        }catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(flag == 1){
                if(i == -2 || i1 == -2 || i2 == -2 || i3 == -2 || i31 == -2)
                    return -2;
            }else{
                if(i == -2 || i1 == -2 || i4 == -2 || i41 == -2)
                    return -2;
            }

            if(i == -1 || i1 == -1 || i2 ==-1 || i3 == -1 || i31 == -1 || i4 == -1 || i41 == -1 )
                return -1;
        }
        return i;
    }

    @Override
    public Map selectPurchaseRecordByWPPID(int wppId, int index, int pageSize) {
        String theirDb = mestUtil.getCompanyDB(request);
        Map map = new HashMap();
        int count = iwpd.selectWarehousePurchasePastRecordCount(theirDb,wppId);

        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",iwpd.selectWarehousePurchasePastRecord(theirDb,wppId,(index-1)*pageSize,pageSize));
        return map;
    }

    @Override
    public int updateByWPId(int wpId, int finishStatus)   {
        String theirDb = mestUtil.getCompanyDB(request);
        int i =-2;
        try {
            i = iwpd.updateByWPId(theirDb,wpId,finishStatus);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }
}
